﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

//IMPORTAMOS 
using EventSys.BussinesEntities;
using EventSys.BussinesLogic;
using Microsoft.Practices.Unity;

namespace EventSys.MVC4Web.Controllers
{
 
    public class CarreraController : Controller
    {
        [Dependency]
        public ICarreraService carreraService { get; set; }

        [Dependency]
        public IFacultadService facultadService{ get; set; }

        //
        // GET: /Carrera/

        public ActionResult Index()
        {
            //var listaCarrera = carreraService.GetFromAllCarrera();
            var listaCarrera = carreraService.GetFromAllCarreraByCriterio("");
            return View(listaCarrera);
        }

        [HttpPost]
        public ActionResult Index(string criterio)
        {
            var buscarCarrera = carreraService.GetFromAllCarreraByCriterio(criterio);

            return View(buscarCarrera);
        }

        //  // GET: /Carrera/Create
        public ActionResult _Create()
        {
            var facultad = facultadService.GetFromAllFacultad();
            ViewData["idFacultad"] = new SelectList(facultad, "idFacultad", "Facultad1");

            return PartialView("_Create");
        }

        //  // POST: 
        [HttpPost]
        public ActionResult _Create(Carrera collection)
        {
            var msg = "ERROR";
            var rpt = new { ok = false, msg = "  "+msg + ",   El Nombre este Repetido"};
            try
            {
                if (validarCarrera(collection) != true)
                {
                    carreraService.RegistrarCarrera(collection);
                    rpt = new
                    {
                        ok = true,
                        msg = "Se registro Correctamente"
                    };
                }
                return Json(rpt, JsonRequestBehavior.AllowGet);//nos permite enviar el mensaje
            }
            catch (SystemException)
            {
                return Json(rpt, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Create()
        {
            var facultad = facultadService.GetFromAllFacultad();
            ViewData["idFacultad"] = new SelectList(facultad, "idFacultad", "Facultad1");

            return View();
        }

        //  // POST: 
        [HttpPost]
        public ActionResult Create(Carrera collection)
        {
            try
            {
                // TODO: Add insert logic here
                carreraService.RegistrarCarrera(collection);

                //return PartialView("_Create");
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View(collection);
            }
        }


        // GET: /Carrera/Edit/5
        public ActionResult Edit(int id)
        {
            var carrera = carreraService.CarreraById(id);
            var facultad = facultadService.GetFromAllFacultad();
            ViewData["idFacultad"] = new SelectList(facultad, "idFacultad", "Facultad1",carrera.idFacultad);

            return View(carrera);
        }

        // POST: /Carrera/Edit/5
        [HttpPost]
        public ActionResult Edit(int id,Carrera carrera)
        {
            try
            {
                // TODO: Add update logic here
                carreraService.EditarCarrera(carrera);
                return RedirectToAction("Index");//View(carrera);
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View(carrera);
            }
        }

        ///// para q se actualice el combo de facultades //////
        //[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult refreshFacultad()
        {
            var facultad = facultadService.GetFromAllFacultad();
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (Facultad p in facultad)
            {
                list.Add(new SelectListItem() { Value = p.idFacultad.ToString(), Text = p.Facultad1});
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        /// ///////para validar el Carrera Repetida/////
        public bool validarCarrera(Carrera carrera)
        {
            var list = carreraService.GetFromAllCarrera().ToList();
            try
            {
                var query = from c in list select c.Carrera1;

                foreach (var item in query)
                {
                    if (item == carrera.Carrera1)
                    {
                        return true;
                    }
                }
            }
            catch (SystemException) { }
            return false; 
        }

    }
}
